package com.software.loginregister.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.software.loginregister.R;
import com.software.loginregister.entity.Works;
import java.util.List;

public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> {

    private Context context;
    private List<Works> historyWorks;
    private OnItemClickListener onItemClickListener;

    public HistoryAdapter(Context context, List<Works> historyWorks, OnItemClickListener listener) {
        this.context = context;
        this.historyWorks = historyWorks;
        this.onItemClickListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_history_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Works work = historyWorks.get(position);
        holder.tvHistoryTitle.setText(work.getWorksName());

        // 使用 Glide 加载图片
        Glide.with(context)
                .load(work.getPhotoUrl())
                .placeholder(R.drawable.ic_food)
                .error(R.drawable.ic_food)
                .into(holder.ivCover);

        holder.itemView.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(work);
            }
        });
    }

    @Override
    public int getItemCount() {
        return historyWorks.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivCover;
        TextView tvHistoryTitle;

        ViewHolder(View itemView) {
            super(itemView);
            ivCover = itemView.findViewById(R.id.iv_cover);
            tvHistoryTitle = itemView.findViewById(R.id.tv_history_title);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(Works work);
    }
}